package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.model.TbContent;
import com.dongdongshop.service.ContentService;
import com.dongdongshop.uitl.JwtUtil;
import com.dongdongshop.vo.ContentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("content")
public class ContentController {

    @Autowired
    private ContentService contentService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private JwtUtil jwtUtil;

    @PostMapping
    public Result getAll(){
        List<ContentVO> contentVOList = contentService.getAll();
        return Result.OK().setData(contentVOList);
    }

    @PostMapping("addContent")
    public Result addContent(@RequestBody ContentVO vo){
        contentService.addContent(vo);
        return Result.OK();
    }

    @PostMapping("toUpdate")
    public Result toUpdate(@RequestParam Long id){
        ContentVO vo = contentService.toUpdate(id);
        return Result.OK().setData(vo);
    }

    @PostMapping("updateContent")
    public Result updateContent(@RequestBody ContentVO vo){
        contentService.updateContent(vo);
        return Result.OK();
    }

    //删除
    @DeleteMapping
    public Result deleteBatch(@RequestParam Long[] ids){
        try {
            contentService.deleteBatch(ids);
            return Result.OK();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ER();
        }
    }

    //轮播图
    @PostMapping("selectByIdLunBo")
    public Result selectByIdLunBo(Long categoryId){
        try {
            List<TbContent> contentList = contentService.selectByIdLunBo(categoryId);
            return Result.OK().setData(contentList);
        }catch (Exception e){
            return Result.ER();
        }
    }

    //今日上新
    @PostMapping("selectByIdNew")
    public Result selectByIdNew(Long categoryId){
        try {
            List<TbContent> contentList = contentService.selectByIdNew(categoryId);
            return Result.OK().setData(contentList);
        }catch (Exception e){
            return Result.ER();
        }
    }

    //猜你喜欢
    @PostMapping("selectByIdLove")
    public Result selectByIdLove(Long categoryId){
        try {
            List<TbContent> contentList = contentService.selectByIdLove(categoryId);
            return Result.OK().setData(contentList);
        }catch (Exception e){
            return Result.ER();
        }
    }
    }


